class Solution
{
public:
    vector<vector<int>> floodFill(vector<vector<int>> &image, int sr, int sc, int color)
    {
        if (image[sr][sc] == color)
        {
            return image;
        }
        int m = image.size();
        int n = image[0].size();
        int originColor = image[sr][sc];
        queue<pair<int, int>> q;

        image[sr][sc] = color;
        q.push({sr, sc});
        while (!q.empty())
        {
            auto [x, y] = q.front();
            q.pop();
            if (x > 0 && image[x - 1][y] == originColor)
            {
                image[x - 1][y] = color;
                q.push({x - 1, y});
            }
            if (y > 0 && image[x][y - 1] == originColor)
            {
                image[x][y - 1] = color;
                q.push({x, y - 1});
            }
            if (x < m - 1 && image[x + 1][y] == originColor)
            {
                image[x + 1][y] = color;
                q.push({x + 1, y});
            }
            if (y < n - 1 && image[x][y + 1] == originColor)
            {
                image[x][y + 1] = color;
                q.push({x, y + 1});
            }
        }
        return image;
    }
};